#define _CRT_SECURE_NO_WARNINGS 1
using pii = pair<int, int>;
class Comp
{
public:
    bool operator()(pii a, pii b)
    {
        return a.second > b.second;
    }
};
class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k)
    {
        unordered_map<int, int> unmap;
        for (auto& a : nums)
        {
            unmap[a]++;
        }
        priority_queue<pii, vector<pii>, Comp> pq;
        for (auto& a : unmap)
        {
            if (pq.size() < k)
            {
                pq.push(a);
            }
            else
            {
                if (a.second > pq.top().second)
                {
                    pq.pop();
                    pq.push(a);
                }
            }
        }
        vector<int> ret;
        while (!pq.empty())
        {
            ret.push_back(pq.top().first);
            pq.pop();
        }
        return ret;
    }
};